<?php
require_once('auth.php');
//Start session

if(isset($_POST['ft']) && trim($_POST['pc'])!='' && trim($_POST['pd'])!=''){
	session_start();
	
	//Include database connection details
	require_once('inc/config.inc');
	
	
	
	//Connect to mysql server
	$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	if(!$link) {
		die('Failed to connect to server: ' . mysql_error());
	}
	
	//Select database
	$db = mysql_select_db(DB_DATABASE);
	if(!$db) {
		die("Unable to select database");
	}
	
	//Common Data //
	$product_category = $_POST['pc'];
	$product_desc = $_POST['pd'];
	$added_by = $_POST['ab'];
	$added_on = $_POST['ao'];
	$added_by2 = $_POST['ab2'];
	$added_on2 = $_POST['ao2'];
	////////////////
	$product_name = $_POST['pn'];
	
	
	$product_quantity = $_POST['pq'];
	$product_uom = $_POST['pu'];
	$product_size = $_POST['ps'];
	$product_iprice = $_POST['pip'];
	$product_fprice = $_POST['pfp'];
	$currency = $_POST['currency'];
	
	
	
	mysql_query ('SET NAMES utf8');
	mysql_query('SET CHARACTER_SET utf8');
	
	switch($_POST['ft']){
		case 'addcat':  $insert=mysql_query("INSERT INTO product_cat(cat_id,name, description,added_on,added_by) VALUES(null,'$product_category','$product_desc','$added_on','$added_by') ");
						break;
		case 'addproduct':
						$insert=mysql_query("INSERT INTO products(product_id,name,quantity,uom,size,currency,initial_price,final_price,category, description,added_on,added_by) VALUES(null,'$product_name','$product_quantity','$product_uom','$product_size','$currency','$product_iprice','$product_fprice','$product_category','$product_desc','$added_on2','$added_by2') ");
						break;
	}
	
	if($insert){
		
		
		?>
		<div class="alert alert-success">
		  <button type="button" class="close" data-dismiss="alert">&times;</button>
		  <strong>Success</strong> Data has been added successfully to the System.
		</div>
		
		<?php
		}
		else {
		?>
		<div class="alert alert-danger">
		  <button type="button" class="close" data-dismiss="alert">&times;</button>
		  <strong>Failed</strong> Data not added to the system.<?php echo mysql_error(); ?>
		</div>
		<?php
			
			}
			
}
			?>
		
